package org.fronczak.anita.berlinclock.view;

import org.fronczak.anita.berlinclock.model.BerlinClockTime;

public class BerlinClockTimeTextRepresentation {

    private static final String LIGHT_OFF = "O";
    private static final String YELLOW_LIGHT = "Y";
    private static final String RED_LIGHT = "R";
    private static final String ROW_DELIMITER = " ";

    private BerlinClockTime berlinClockTime;

    public BerlinClockTimeTextRepresentation(BerlinClockTime berlinClockTime) {
        this.berlinClockTime = berlinClockTime;
    }

    public String getRepresentation() {
        return getSecondsRepresentation() + ROW_DELIMITER + getFirstRowHoursRepresentation() + ROW_DELIMITER
                + getSecondRowHoursRepresentation() + ROW_DELIMITER + getFirstRowMinutesRepresentation()
                + ROW_DELIMITER + getSecondRowMinutesRepresentation();
    }

    private String getSecondsRepresentation() {
        return berlinClockTime.isSecondsLampLit() ? YELLOW_LIGHT : LIGHT_OFF;
    }

    private String getFirstRowHoursRepresentation() {
        StringBuilder result = new StringBuilder();
        int i = 0;
        for (; i < BerlinClockTime.NUMBER_OF_LAMPS_IN_FIRST_HOURS_ROW
                && i < berlinClockTime.getNumberOfFirstRowHourLampsLit(); ++i) {
            result.append(RED_LIGHT);
        }
        for (; i < BerlinClockTime.NUMBER_OF_LAMPS_IN_FIRST_HOURS_ROW; ++i) {
            result.append(LIGHT_OFF);
        }
        return result.toString();
    }

    private String getSecondRowHoursRepresentation() {
        StringBuilder result = new StringBuilder();
        int i = 0;
        for (; i < BerlinClockTime.NUMBER_OF_LAMPS_IN_SECOND_HOURS_ROW
                && i < berlinClockTime.getNumberOfSecondRowHourLampsLit(); ++i) {
            result.append(RED_LIGHT);
        }
        for (; i < BerlinClockTime.NUMBER_OF_LAMPS_IN_SECOND_HOURS_ROW; ++i) {
            result.append(LIGHT_OFF);
        }
        return result.toString();
    }

    private String getFirstRowMinutesRepresentation() {
        StringBuilder result = new StringBuilder();
        int i = 0;
        for (; i < BerlinClockTime.NUMBER_OF_LAMPS_IN_FIRST_MINUTES_ROW
                && i < berlinClockTime.getNumberOfFirstRowMinuteLampsLit(); ++i) {
            if (i % 3 == 2) {
                result.append(RED_LIGHT);
            } else {
                result.append(YELLOW_LIGHT);
            }
        }
        for (; i < BerlinClockTime.NUMBER_OF_LAMPS_IN_FIRST_MINUTES_ROW; ++i) {
            result.append(LIGHT_OFF);
        }
        return result.toString();
    }

    private String getSecondRowMinutesRepresentation() {
        StringBuilder result = new StringBuilder();
        int i = 0;
        for (; i < BerlinClockTime.NUMBER_OF_LAMPS_IN_SECOND_MINUTES_ROW
                && i < berlinClockTime.getNumberOfSecondRowMinuteLampsLit(); ++i) {
            result.append(YELLOW_LIGHT);
        }
        for (; i < BerlinClockTime.NUMBER_OF_LAMPS_IN_SECOND_MINUTES_ROW; ++i) {
            result.append(LIGHT_OFF);
        }
        return result.toString();
    }

}
